package com.jdd.modules.through.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdd.modules.dashboard.dto.ThroughDataDTO;
import com.jdd.modules.through.entity.Through;
import com.jdd.modules.through.mapper.ThroughMapper;
import com.jdd.modules.through.service.IThroughService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Description: 通行配置表
 * @Author: jeecg-boot
 * @Date:   2022-09-30
 * @Version: V1.0
 */
@Service
@Slf4j
public class ThroughServiceImpl extends ServiceImpl<ThroughMapper, Through> implements IThroughService {

    @Resource
    private ThroughMapper throughMapper;

    @Override
    public List<ThroughDataDTO> getThroughData(List<String> projectCodes) {
        List<ThroughDataDTO> returnList = new LinkedList<>();
        Date now = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        now = calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH,-7);
        List<ThroughDataDTO> list = throughMapper.getThroughData(calendar.getTime(),now,projectCodes);
        int day = 0;
        int listIndex = 0;
        while(day<7)
        {
            if(listIndex<list.size())
            {
                ThroughDataDTO item = list.get(listIndex);
                if(item.getTime().toString().equals(calendar.getTime().toString())){
                    returnList.add(item);
                    listIndex++;
                    day++;
                    calendar.add(Calendar.DAY_OF_MONTH,1);
                    continue;
                }
            }
            returnList.add(new ThroughDataDTO(calendar.getTime(),0));
            day++;
            calendar.add(Calendar.DAY_OF_MONTH,1);
        }
        return returnList;
    }
}
